import Settings from '../config/Settings.js';

export default class Obstacle {
  constructor(game, type, options = {}) {
    this.game = game;
    this.type = type;
    this.x = options.x || game.canvas.width;
    this.y = options.y || 0;
    
    const config = Settings.obstacles.types[type];
    this.width = config.width;
    this.height = config.height;
    this.color = config.color;
    
    // 移动平台特有属性
    if (type === 'movingPlatform') {
      this.initialY = this.y;
      this.moveDirection = 1;
      this.amplitude = config.amplitude;
      this.speed = config.speed;
    }
    
    // 可破坏平台特有属性
    if (type === 'breakablePlatform') {
      this.breakTimer = 0;
      this.breaking = false;
      this.cracks = [];
    }
  }
  
  update() {
    switch(this.type) {
      case 'spike':
        this.x -= Settings.platform.speed;
        break;
        
      case 'movingPlatform':
        this.x -= Settings.platform.speed;
        // 上下移动
        this.y += this.speed * this.moveDirection;
        if (Math.abs(this.y - this.initialY) > this.amplitude) {
          this.moveDirection *= -1;
        }
        break;
        
      case 'breakablePlatform':
        this.x -= Settings.platform.speed;
        if (this.breaking) {
          this.breakTimer++;
          if (this.breakTimer >= Settings.obstacles.types.breakablePlatform.breakTime) {
            return true; // 标记为需要移除
          }
          // 生成裂纹
          if (this.breakTimer % 10 === 0 && this.cracks.length < 5) {
            this.cracks.push({
              x: Math.random() * this.width,
              y: Math.random() * this.height,
              length: Math.random() * 20 + 10,
              angle: Math.random() * Math.PI
            });
          }
        }
        break;
    }
    
    return false;
  }
  
  render(ctx) {
    ctx.save();
    
    switch(this.type) {
      case 'spike':
        // 绘制三角形地刺
        ctx.beginPath();
        ctx.fillStyle = Settings.obstacles.types.spike.sprite.color;
        ctx.moveTo(this.x, this.y + this.height);
        ctx.lineTo(this.x + this.width/2, this.y);
        ctx.lineTo(this.x + this.width, this.y + this.height);
        ctx.closePath();
        ctx.fill();
        break;
        
      case 'movingPlatform':
        // 绘制移动平台
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        // 添加纹理
        ctx.fillStyle = 'rgba(0,0,0,0.1)';
        for (let i = 0; i < this.width; i += 20) {
          ctx.fillRect(this.x + i, this.y, 2, this.height);
        }
        break;
        
      case 'breakablePlatform':
        // 绘制可破坏平台
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        // 绘制裂纹
        if (this.breaking) {
          ctx.strokeStyle = Settings.obstacles.types.breakablePlatform.crackColor;
          ctx.lineWidth = 2;
          for (const crack of this.cracks) {
            ctx.beginPath();
            ctx.moveTo(this.x + crack.x, this.y + crack.y);
            ctx.lineTo(
              this.x + crack.x + Math.cos(crack.angle) * crack.length,
              this.y + crack.y + Math.sin(crack.angle) * crack.length
            );
            ctx.stroke();
          }
        }
        break;
    }
    
    ctx.restore();
  }
  
  checkCollision(player) {
    // 基础碰撞检测
    const collision = (
      player.x + player.width/2 > this.x &&
      player.x - player.width/2 < this.x + this.width &&
      player.y + player.height > this.y &&
      player.y < this.y + this.height
    );
    
    if (!collision) return false;
    
    // 特殊处理
    switch(this.type) {
      case 'spike':
        // 地刺造成伤害
        player.takeDamage();
        return false;
        
      case 'breakablePlatform':
        // 开始破碎倒计时
        if (!this.breaking) {
          this.breaking = true;
          return true;
        }
        return this.breakTimer < Settings.obstacles.types.breakablePlatform.breakTime;
        
      default:
        return true;
    }
  }
} 